package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.activity.base.MainTabActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.myapplication.MyApplication;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.utils.SystemUtil;
import com.tool.utils.utils.LogUtils;
import com.tool.utils.utils.ToastUtils;
import com.tool.utils.utils.UtilPreference;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private final String TAG = "LoginActivity";
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.btn_register)
    TextView btnRegister;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        MyActivityManager.getInstance().addActivity(this);
        showView(R.id.titleback_btn_back, false);
        initView();
    }


    private void initView() {
        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.btn_forget_password).setOnClickListener(this);
        findViewById(R.id.btn_register).setOnClickListener(this);

        tvVersion.setText("由商博士提供技术支持" + SystemUtil.getVerName(mContext));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.btn_forget_password:
                startActivity(new Intent(mContext, ActivityForgotLoginPwd.class));
                break;
            case R.id.btn_register:
                startActivity(new Intent(mContext, ActivityRegister.class));
                break;
            default:
                break;
        }
    }

    private void login() {
        final String username = getTvText(R.id.tv_username);
        final String password = getTvText(R.id.tv_password);
        if (StringUtil.isNull(username)) {
            ToastUtils.showShort(mContext, "用户名不能为空！");
            return;
        } else if (StringUtil.isNull(password)) {
            ToastUtils.showShort(mContext, "密码不能为空！");
            return;
        }

        showProgress("加载中...");
        RequestParams params = new RequestParams();
        params.add("username", username);
        params.add("password", password);
        HttpUtil.get(Config.SBS_LOGIN, params, new HttpUtil.RequestListener() {
            @Override
            public void success(String response) {
                disShowProgress();
                try {
                    JSONObject result = new JSONObject(response);

                    if (!result.optString("errorCode").equals("0000")) {
                        showErrorMsg(result.optString("errorMsg"));
                        return;
                    }

                    UtilPreference.saveString(mContext, "username", getTvText(R.id.tv_username));
                    UtilPreference.saveString(mContext, "password", getTvText(R.id.tv_password));
                    MyApplication.getmInstance().setLoginMEntity(response);


                    PushServiceFactory.getCloudPushService().bindAccount(username, new CommonCallback() {
                        @Override
                        public void onSuccess(String s) {
                            LogUtils.e(TAG, s);
                        }

                        @Override
                        public void onFailed(String s, String s1) {

                        }
                    });


                    gotoMainPage();
                    finish();
                    return;


                } catch (Exception e) {
                    Log.e(TAG, "doLogin() Exception: " + e.getMessage());
                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }
        });
    }

    private void gotoMainPage() {
        startActivity(new Intent(mContext, MainTabActivity.class));
        finish();
    }

}
